new Vue({
  el: '#app',
  data() {
    return {
      form: {
        Start_Date: '',
        End_Date: '',
        Category_SerialNumber: '',
        Supplier_SerialNumber: '',
        Store_SerialNumber: '',
        token: $.cookie("token"),
        page: 1,			//页数
        limit: 10,		//每页数据条数
      },
      form1: {
        Start_Date: '',
        End_Date: '',
        Article_Name: '',
        Row_Status: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        token: $.cookie("token"),
        page: 1,			//页数
        limit: 10,		//每页数据条数
      },
      total: 0,// 总条数
      total1: 0,// 总条数
      radio: '',
      formData: {},
      activeName: '',
      category: [],//分类查询--类别
      supplier: [],//分类查询--供应商
      supplier1: [],//订单分配--供应商
      multipleSelection: [],//订单分配--数据
      tableData: [],		//表格数据
      form3: [{//分配供应商--日志生成
        Supplier_SerialNumber: '',
        Sales_Order_Detail_SerialNumber: '',
        Store_SerialNumber: '',
        Article_SerialNumber: '',
        Quantity: '',
        Enterprise_Info_SerialNumber: ''
      }],
      store: [],
      price: {
        Sales_Amount: 0,   //出库金额
        Collection_Amount: 0,  //出库已收金额
        Remain_Amount: 0,  //出库未收金额
        Purchase_Amount: 0, //应付金额
        Paid_Amount: 0,   //已付金额
        Unpaid_Amount: 0,  //未付金额
        Receive_Type: 0,//未分配供应商销售订单数
        Profit_Amount: 0,//利润
        Profit_Rate: 0,//利率
        Cost_Profit_Rate: 0//成本利率
      },
      dialogVisible2: false,//分配供应商窗口
      dialogForm: false,
      loading: false,//节流
      pickerOptions: pickerOptionsTwo,
      time: '',
      WareUser:[{
        label:'全部',
        value:''
      },{
        label:'用户流水',
        value:'0'
      },
      {
        label:'仓库流水',
        value:'1'
      }]
    }
  },
  mounted() {
    this.getList();
    this.getData(this.form);
  },
  computed: {
    formatTime1() {
      return function (times) {
        if (times) {
          let date = new Date(times);
          let year = date.getFullYear();
          let month = date.getMonth() + 1;
          let day = date.getDate();
          if (month < 10) {
            month = "0" + month;
          }
          if (day < 10) {
            "0" + day
          }
          return `${year}-${month}-${day}`;
        } else { return '' }
      }
    },
    //去.0
    ZeroRemoval() {
      return function (params) {
        params = params.replace('.0', '')
        return params
      }
    },
    //千分位处理
    thousands() {
      return function (params) {
        params = Number(params).toFixed(2)
        return params.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      }
    },
    //百分比处理
    toPercent() {
      return function (point) {
        point = Number(point * 100).toFixed(2);
        return point += "%";
      }
    },
  },
  methods: {
    getData(data) {
      let _this = this;
      $.ajax({
        type: 'POST',
        url: DomainName + "/getReconciliationPrice1Action",
        data,
        success: function (res) {
          _this.tableData = res.Data.tabledata
          _this.price = res.Total
          _this.total = res.Data.limit
          console.log(res);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    ChangeWareUser(e){
      if(e=='0'){
        console.log('用户')
      }else{
        console.log('仓库')
      }
    },
    accAdd(arg1, arg2) {	//浮点型相加
      var r1, r2, m;
      try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
      try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
      m = Math.pow(10, Math.max(r1, r2))
      return (arg1 * m + arg2 * m) / m
    },
    getList() {
      let _this = this
      //获取门店名称
      $.ajax({
        type: 'POST',
        url: DomainName + "/getInquiryAction",
        data: {
          Data: "store_info",
          Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          _this.store = res
          // res.forEach(item => {
          //   _this.store.push(item)
          //   _this.listitem.push(item.Store_SerialNumber)
          // })
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      //获取类别
      $.ajax({
        type: 'POST',
        url: DomainName + "/GoodsFileInquiry",
        data: {
          Data: "category",
          token: $.cookie("token"),
          isNullEnterprise:true
        },
        success: function (res) {
          _this.category = res
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      //获取供应商
      $.ajax({
        type: 'POST',
        url: DomainName + "/getInquiryAction",
        data: {
          Data: "supplier_info",
          Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          _this.supplier = res
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    //表格展开
    handleChildren(row) {
      console.log(row.Children.article_info);
      _this = this
      $.ajax({
        url: DomainName + '/getReconciliationPriceDetailAction',
        type: 'POST',
        data: {
          Article_SerialNumber: row.Article_SerialNumber,
          Supplier_SerialNumber: row.Supplier_SerialNumber,
          token: $.cookie("token")
        },
        success: function (data) {
          row.Children.article_info = data
          console.log(row.Children.article_info);
        },
        dataType: "json"
      });
      // }

    },
    // 改变日期
    DateChange() {
      this.form.Start_Date = this.time[0]
      this.form.End_Date = this.time[1]
      console.log(this.form.Start_Date, this.form.End_Date)
      this.radio == '0'
      this.getData(this.form);
    },
    handlechange() {
      if (this.radio == '1') {
        this.form.Start_Date = this.formatTime1(new Date().getTime() - 60 * 60 * 24 * 365 * 1000) + ' 15:00:00',
          this.form.End_Date = this.formatTime1(new Date().getTime()) + ' 15:00:00',
          this.getData(this.form);
      } else if (this.radio == '2') {
        this.form.Start_Date = this.formatTime1(new Date().getTime() - 60 * 60 * 24 * 30 * 1000) + ' 15:00:00',
          this.form.End_Date = this.formatTime1(new Date().getTime()) + ' 15:00:00',
          this.getData(this.form);
      } else if (this.radio == '3') {
        this.form.Start_Date = this.formatTime1(new Date().getTime() - 60 * 60 * 24 * 1000) + ' 15:00:00',
          this.form.End_Date = this.formatTime1(new Date().getTime()) + ' 15:00:00',
          this.getData(this.form);
      }
    },
    handleclassify() {//分类查询
      this.getData(this.form);
    },
    handleSizeChange(val) {//分页查询
      this.form.page = 1;
      this.form.limit = val;
      this.getData(this.form)
    },
    handleSizeChange1(val) {//分页查询
      this.form1.page = 1;
      this.form1.limit = val;
      this.getSupplier(this.form1)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getData(this.form)
    },
    handleCurrentChange1(val) {
      this.form1.page = val;
      this.getSupplier(this.form1)
    },
    // 获取未分配供应商
    getSupplier(data) {
      let _this = this
      $.ajax({
        type: 'POST',
        data,
        url: DomainName + '/getSalesOrderAllAction',
        success: function (Result) {
          let arr = []
          _this.multipleSelection = Result.tableData;
          _this.total1 = parseInt(Result.limit)
          _this.multipleSelection.forEach(item => {
            arr.push(item.Article_SerialNumber)
          })
          console.log(arr)
          console.log(_this.multipleSelection)
          _this.form3.forEach(item => {
            _this.multipleSelection.forEach(item1 => {
              console.log(item)
              console.log(item1)
              item.Enterprise_Info_SerialNumber = item1.Enterprise_Info_SerialNumber
              item.Sales_Order_Detail_SerialNumber = item1.Sales_Order_Detail_SerialNumber
              item.Store_SerialNumber = item1.Store_SerialNumber
              item.Article_SerialNumber = item1.Article_SerialNumber
              item.Quantity = item1.Quantity
              item.Supplier_SerialNumber = ''
            })
          })
          console.log(_this.form3)
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      })
    },
    DistributeSupplier() {//分配供应商
      //1.判断该订单是否分配供应商
      let _this = this
      // this.SalesOrderSerialNumber = row.Sales_Order_SerialNumber
      // console.log(row.Enterprise_Article_SerialNumber, row.Enterprise_Info_SerialNumber)
      //初始化
      this.multipleSelection = []
      this.supplier = []
      this.form3 = []
      this.dialogVisible2 = true
      // 获取卖此商品供应商
      this.form1.Row_Status = 1
      this.getSupplier(_this.form1)
      //获取数据
      // this.multipleSelection.push(row)
      // this.form3 = {
      //   Enterprise_Info_SerialNumber: row.Enterprise_Info_SerialNumber,
      //   Sales_Order_Detail_SerialNumber: row.Sales_Order_Detail_SerialNumber,
      //   Store_SerialNumber: row.Store_SerialNumber,
      //   Article_SerialNumber: row.Article_SerialNumber,
      //   Quantity: row.Quantity,
      //   Supplier_SerialNumber: ''
      // }

    },
    GeneratePurchase(row) {//生成采购订单
      if (row.Purchase_Amount == '0') {
        $.ajax({
          type: 'POST',
          data: {
            Sales_Order_SerialNumber: row.Sales_Order_SerialNumber,
            token: $.cookie("token")
          },
          url: DomainName + '/getPurchaseOrderAddAction',
          success: function (data) {
            if (data.Status === 'success') {
              _this.getData(_this.form)
            } else if (data.Status === 'fail404') {
              _this.$message({
                type: 'info',
                message: '该供应商没有采购价格单，请先添加采购价格单'
              });
            } else {
              _this.$message({
                type: 'info',
                message: '提交失败'
              });
            }
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
          dataType: "json"
        });
      } else {
        _this.$message({
          type: 'info',
          message: '已生成采购订单'
        });
      }
    },
    submitForm() {//生成供应商提交信息
      let _this = this
      _this.loading = true
      console.log(_this.form3)
      // 分配供应商
      // $.ajax({
      //   type: 'POST',
      //   data: {
      //     Data: JSON.stringify({
      //       sales_order_supplier: [_this.form3]
      //     }),
      //     token: $.cookie("token")
      //   },
      //   url: DomainName + '/getAddAction',
      //   success: function (data) {
      //     console.log(data)
      //     _this.$message.success('分配供应商成功')
      //     _this.dialogVisible2 = false,
      //       _this.getData(_this.form)
      //   },
      //   complete: function (XMLHttpRequest) {
      //     _this.loading = false
      //     if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
      //       window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
      //     }
      //   },
      //   dataType: "json"
      // });
    },
  },
})